<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var subarraysDivByK = function (A, K) {
      var m = [1, ...new Array(K - 1).fill(0)];
      return [{ s: 0, n: 0 }, ...A].reduce(({ s, n }, a) => {
        s += a;
        n += m[(s % K + K) % K]++;
        return { s, n }
      }).n;
    };
    console.log(subarraysDivByK([4, 5, 0, -2, -3, 1], 5))
  </script>
</body>

</html>